package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Room;
import cn.wolfcode.domain.Student;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.RoomService;
import cn.wolfcode.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    @RequiresPermissions("room:list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        List<Room> rooms = roomService.selAll();
        //遍历人数
        for (Room room : rooms) {
            int person = studentService.selectCountStudents(room.getId());
            room.setPerson(person);
            roomService.saveOrUpdate(room);
        }
        PageInfo<Room> pageInfo = roomService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "room/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("room:delete")
    public String deleteById(Long id) {
        roomService.deleteById(id);
        return "redirect:/room/list";
    }

//    @RequestMapping("/input")
//    public String input(Long id, Model model) {
//        if (id != null) {
//            //遍历当前用户信息
//            Room room = roomService.selRoomById(id);
//            model.addAttribute("room", room);
//        }
//        return "room/input";
//    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions("room:saveOrUpdate")
    public String saveOrUpdate(Room room) {
        roomService.saveOrUpdate(room);
        return "redirect:/room/list";
    }
}
